package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.store.StoreDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.intf.cart.IStoreOrderCartInnerServiceSMO;
import com.java110.intf.goods.IProductSpecDetailInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("queryAdminStoreOrderCartBmoImpl")
public class QueryAdminStoreOrderCartBmoImpl  implements IMallCommonApiBmo {

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;

    @Autowired
    private IProductSpecDetailInnerServiceSMO productSpecDetailInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.jsonObjectHaveKey(reqJson, "page", "请求中未包含page信息");
        Assert.jsonObjectHaveKey(reqJson, "row", "请求中未包含row信息");
        Assert.jsonObjectHaveKey(reqJson, "adminStoreTypeCd", "请求中未包含adminStoreTypeCd信息");

        String adminStoreTypeCd = reqJson.getString("adminStoreTypeCd");
        if (!StoreDto.STORE_TYPE_ADMIN.equals(adminStoreTypeCd)) {
            throw new CmdException("身份不合法");
        }
        reqJson.remove("storeId");
    }

    @Override
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {

        StoreOrderCartDto storeOrderCartDto = BeanConvertUtil.covertBean(reqJson,StoreOrderCartDto.class);
        int count = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCartsCount(storeOrderCartDto);

        List<StoreOrderCartDto> storeOrderCartDtos = null;
        if (count > 0) {
            storeOrderCartDtos = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCartAdminInfo(storeOrderCartDto);
            String[] shopId = null;
            for (int storeOrderCartDtosIndex = 0; storeOrderCartDtosIndex < storeOrderCartDtos.size(); storeOrderCartDtosIndex++) {
                shopId = new String[storeOrderCartDtos.size()];
                shopId[storeOrderCartDtosIndex] = storeOrderCartDtos.get(storeOrderCartDtosIndex).getShopId();
            }
            //查询关联shop
            List<ShopDto> shopDtos = null;
            if (null != shopId && shopId.length > 0) {
                shopDtos = shopInnerServiceSMOImpl.getOrderShopInfo(shopId);
            }
            //匹配商品对应的中文名称
            if (null != shopDtos && shopDtos.size() > 0) {
                for (StoreOrderCartDto storeOrder : storeOrderCartDtos) {
                    for (ShopDto shopDto : shopDtos) {
                        if (storeOrder.getShopId().equals(shopDto.getShopId())) {
                            storeOrder.setShopName(shopDto.getShopName());
                        }
                    }
                }
            }
            //freshOrderCart(storeOrderCartDtos);
        } else {
            storeOrderCartDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeOrderCartDto.getRow()), count, storeOrderCartDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        context.setResponseEntity(responseEntity);

    }
}
